<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
     <script type="text/javascript" charset="utf-8" src="/javascripts/jquery-2.1.0.min.js"></script>
	<title>新闻维护</title>
	<style type="text/css">
		*{
			margin: 0px;
			padding: 0px;
		}
		html,
		body{
			background: #f2f2f2;
			height:  100%;
		}
		#page{
			width: 1200px;
			height:  100%;
			margin:0px auto;
			background: white;
		}
		input{
			width: 400px;
			height: 40px;
			margin: 20px 30px;
		}
        .sub_but{
        	display: inline-block;
        	width: 200px;
        	line-height: 40px;
        	text-align: center;
        	background: orange;
        }
       ul li{
       	 list-style: none;
       	 padding-left: 30px;
       	 margin-top: 20px;
       }
       .title{
       	display: inline-block;
       	width: 908px;
       }
       .del{
            display: inline-block;
        	width: 200px;
        	line-height: 40px;
        	text-align: center;
        	background: orange;
       }
	</style>
</head>
<body>
     <div id="page">
     	<div>
     	   <input type="" id="article_title" name="" placeholder="文章标题">
           <input type="" id="link_url" name="" placeholder="文章链接">
           <label class="sub_but" onclick="sendAction()">确认</label>
     	</div>
        <div id="content">
        	<ul>
        		
        	</ul>
        </div>
     </div>
     <script type="text/javascript">
       $(function(){
         
           getAllNews()

       })
       function getAllNews(){
       	  $.ajax({
                url: "/news/allRelease",
                type: "get",
                dataType: "json",
                success: succesFunc,
                error: errorFunc
            });

            function succesFunc(resquest) {
            	var shtml="";
                for (var i = 0; i < resquest.data.length; i++) {
                	shtml+='<li><label class="title">'+resquest.data[i].title+'</label><span class="del" onclick="delAction('+resquest.data[i].id+')">删除</span></li>'
                }
                $("#content ul").empty().append(shtml);
            }

            function errorFunc(resquest) {
            }
       }
       function delAction(id){
           $.ajax({
                url: "/news/del/"+id,
                type: "get",
                dataType: "json",
                success: succesFunc,
                error: errorFunc
            });

            function succesFunc(resquest) {
            	getAllNews()
            }

            function errorFunc(resquest) {
            	alert("删除失败")
            }
       }
     	function sendAction(argument) {
     	 	 var jsonData = {
            title: document.getElementById("article_title").value,
            link_url: document.getElementById("link_url").value,
            }
        if (jsonData.title == "") {
            alert("请输入标题");
        } else if (jsonData.link_url == "") {
            alert("请输入连接");
        } 
        console.log(jsonData)

            $.ajax({
                url: "/news/toRelease",
                type: "post",
                dataType: "json",
                data: jsonData,
                success: succesFunc,
                error: errorFunc
            });

            function succesFunc(resquest) {
                console.log(resquest);
                $("#float_view").hide();
               getAllNews()

            }

            function errorFunc(resquest) {
                $("#float_view").hide();
                alert("失败");
            }

     	 }


     </script>
</body>
</html>